package com.akcome.apollo.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akcome.apollo.common.MathUtil;
import com.akcome.apollo.domain.BmpAccount;
import com.akcome.apollo.domain.VStatsWorkersHour;
import com.akcome.apollo.dto.PoolSpeedBean;
import com.akcome.apollo.dto.SpeedData;
import com.akcome.apollo.dto.WorkerScore;
import com.akcome.apollo.dto.WorkerSeries;
import com.akcome.apollo.dto.WorkerSpeedResult;
import com.akcome.apollo.repository.VStatsWorkersHourRepository;
import com.akcome.apollo.service.BmpAccountService;
import com.akcome.apollo.service.VStatsWorkersDayService;
import com.akcome.apollo.service.VStatsWorkersHourService;


/**
 * VIEWService接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VStatsWorkersHourServiceImpl implements VStatsWorkersHourService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VStatsWorkersHourRepository vStatsWorkersHourRepository;
	
	@Autowired
	private VStatsWorkersDayService vStatsWorkersDayService;
	
	@Autowired
	private BmpAccountService bmpAccountService;
	
	@Override
	public VStatsWorkersHour save(VStatsWorkersHour vStatsWorkersHour){
		return vStatsWorkersHourRepository.save(vStatsWorkersHour);
	}
	
	@Override
	public VStatsWorkersHour findOne(Long id){
		return vStatsWorkersHourRepository.findOne(id);
	}
	
	@Override
	public List<WorkerScore> findSpeedByPuid(Long puid,String beginDate,String endDate){
		return vStatsWorkersHourRepository.findSpeedByPuid(puid, beginDate, endDate);
	}
	
	
	@Override
	public WorkerSpeedResult workersSpeed(Long accountId,String type) throws Exception{
		
		WorkerSpeedResult workerSpeedResult = new WorkerSpeedResult();
		List<WorkerSeries> series = new ArrayList<WorkerSeries>();
		BmpAccount bmpAccount = bmpAccountService.findOne(accountId);
		if("hour".equals(type)){
			//查询近24小时内数据
			Date endDate = new Date();
			Date beginDate = DateUtils.addDays(endDate, -1);
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHH");
			List<WorkerScore> workerScores = findSpeedByPuid(accountId, sf.format(beginDate), sf.format(endDate));
			for (WorkerScore workerScore : workerScores) {
				WorkerSeries workerSeries = null;
				for (WorkerSeries s : series) {
					if(s.getName().equals(bmpAccount.getName()+"."+workerScore.getWorkerName())){
						workerSeries = s;
						break;
					}
				}
				boolean isadd = false;
				if(null == workerSeries){
					workerSeries = new WorkerSeries();
					workerSeries.setName(bmpAccount.getName()+"."+workerScore.getWorkerName());
					workerSeries.setData(new ArrayList<Object[]>());
					isadd = true;
				}
				List<Object[]> objects =  workerSeries.getData();
				Double score =MathUtil.div(MathUtil.mul(workerScore.getScore().doubleValue(), Math.pow(256, 4)), Math.pow(10, 12), 2);
				objects.add(new Object[]{sf.parse(""+workerScore.getTime()).getTime(),score});
				workerSeries.setData(objects);
				if(isadd){
					series.add(workerSeries);
				}
			}
			
		}else{
			//查询近一个月
			Date endDate = new Date();
			Date beginDate = DateUtils.addMonths(endDate, -1);
			SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
			List<WorkerScore> workerScores = vStatsWorkersDayService.findSpeedByPuid(accountId, sf.format(beginDate), sf.format(endDate));
			for (WorkerScore workerScore : workerScores) {
				WorkerSeries workerSeries = null;
				for (WorkerSeries s : series) {
					if(s.getName().equals(bmpAccount.getName()+"."+workerScore.getWorkerName())){
						workerSeries = s;
						break;
					}
				}
				boolean isadd = false;
				if(null == workerSeries){
					workerSeries = new WorkerSeries();
					workerSeries.setName(bmpAccount.getName()+"."+workerScore.getWorkerName());
					workerSeries.setData(new ArrayList<Object[]>());
					isadd = true;
				}
				List<Object[]> objects =  workerSeries.getData();
				Double score =MathUtil.div(MathUtil.mul(workerScore.getScore().doubleValue(), Math.pow(256, 4)), Math.pow(10, 12), 2);
				objects.add(new Object[]{sf.parse(""+workerScore.getTime()).getTime(),score});
				workerSeries.setData(objects);
				if(isadd){
					series.add(workerSeries);
				}
			}
			
		}
		workerSpeedResult.setSeries(series);
		return workerSpeedResult;
	}
}
